﻿
<template>
    <section>

        <!--工具条-->
        <el-col :span='24' class='toolbar' style='padding-bottom: 0px;'>
            <el-form :inline='true' :model='filters' @submit.native.prevent>
                <el-form-item>
                    <el-input v-model='filters.name' placeholder='名称/Key'></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type='primary' @click='getDtoPage'>查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type='primary' @click='handleAdd'>新增</el-button>
                </el-form-item>
                <el-button type='danger' @click='batchRemove' :disabled='this.sels.length===0'>批量删除</el-button>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data='users' highlight-current-row v-loading='listLoading' @selection-change='selsChange'
                  style='width: 100%;'
                  sortable='custom' @sort-change='sortChange'>

            <el-table-column type="selection" width="50"> </el-table-column>
            <el-table-column type="index" width="80"></el-table-column>
            <!--<el-table-column prop='ID' label='上位机编号主键' width='' sortable>  </el-table-column>-->
            <el-table-column prop='SWJ_Name' label='上位机名称' width='' sortable>  </el-table-column>
            <el-table-column prop='SWJ_Code' label='上位机代码' width='' sortable>  </el-table-column>
            <!--<el-table-column prop='SWJ_Address' label='上位机地址 ' width='' sortable>  </el-table-column>-->
            <el-table-column prop='SWJ_IP' label='上位机IP' width='' sortable>  </el-table-column>


            <el-table-column prop='SWJ_Port' label='端口号' width='' sortable>  </el-table-column>
            


            <!--<el-table-column prop='SWJ_Remark' label='备注' width='' sortable>  </el-table-column>-->
            <el-table-column prop='SWJ_State' label='状态' width='' sortable>
                <template slot-scope="scope">
                    <el-tag :type="scope.row.SWJ_State|getBindStatus"
                            close-transition>{{scope.row.SWJ_State|getBindText}}</el-tag>
                </template>
            </el-table-column>


            <el-table-column label='操作' width='150'>
                <template scope='scope'>
                    <el-button size='small' @click='handleEdit(scope.$index, scope.row)'> 编辑 </el-button>
                    <el-button type='danger' size='small' @click='handleDel(scope.$index, scope.row)'> 删除 </el-button>
                </template>
            </el-table-column>

        </el-table>

        <!-- 工具条 ,分页 -->
        <el-col :span='24' class='toolbar'>

            <!--<el-pagination layout='prev, pager, next' @current-change='handleCurrentChange' :page-size='50'
                           :total='total' style='float:right;'>
            </el-pagination>-->
            <el-pagination @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="pageIndex"
                           :page-sizes="[ 20, 50, 100]"
                           :page-size="pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="total"
                           style='float:right;'>
            </el-pagination>


        </el-col>

        <!--编辑界面-->
        <el-dialog title='编辑' :visible.sync='editFormVisible' v-model='editFormVisible' :close-on-click-modal='false'>
            <el-form :model='editForm' label-width='100px' :rules='editFormRules' ref='editForm'>

                <!--<el-form-item label='上位机编号主键' prop='ID'>
        <el-input v-model='editForm.ID' auto-complete='off'></el-input>
    </el-form-item>-->


                <el-form-item label='上位机名称' prop='SWJ_Name'>
                    <el-input v-model='editForm.SWJ_Name' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机代码' prop='SWJ_Code'>
                    <el-input v-model='editForm.SWJ_Code' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机地址 ' prop='SWJ_Address'>
                    <el-input v-model='editForm.SWJ_Address' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机IP' prop='SWJ_IP'>
                    <el-input v-model='editForm.SWJ_IP' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='端口号' prop='SWJ_Port'>
                    <el-input v-model='editForm.SWJ_Port' auto-complete='off'></el-input>
                </el-form-item>



                <el-form-item label='备注' prop='SWJ_Remark'>
                    <el-input v-model='editForm.SWJ_Remark' auto-complete='off'
                              type="textarea" :rows="3"></el-input>
                </el-form-item>


                <el-form-item label='状态' prop='SWJ_State'>
                    <template>
                        <el-radio-group v-model="editForm.SWJ_State">
                            <el-radio v-for="item in listState"
                                      :key="item.value"
                                      :label="item.value">{{item.text}}</el-radio>
                        </el-radio-group>
                    </template>
                </el-form-item>


            </el-form>
            <div slot='footer' class='dialog-footer'>
                <el-button @click.native='editFormVisible = false'>取消</el-button>
                <el-button type='primary' @click.native='editSubmit' :loading='editLoading'>提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title='新增' :visible.sync='addFormVisible' v-model='addFormVisible' :close-on-click-modal='false'>
            <el-form :model='addForm' label-width='100px' :rules='addFormRules' ref='addForm'>



                <!--<el-form-item label='上位机编号主键' prop='ID'>
        <el-input v-model='addForm.ID' auto-complete='off'></el-input>
    </el-form-item>-->


                <el-form-item label='上位机名称' prop='SWJ_Name'>
                    <el-input v-model='addForm.SWJ_Name' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机代码' prop='SWJ_Code'>
                    <el-input v-model='addForm.SWJ_Code' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机地址 ' prop='SWJ_Address'>
                    <el-input v-model='addForm.SWJ_Address' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='上位机IP' prop='SWJ_IP'>
                    <el-input v-model='addForm.SWJ_IP' auto-complete='off'></el-input>
                </el-form-item>
                <el-form-item label='端口号' prop='SWJ_Port'>
                    <el-input v-model='addForm.SWJ_Port' auto-complete='off'></el-input>
                </el-form-item>


                <el-form-item label='备注' prop='SWJ_Remark'>
                    <el-input v-model='addForm.SWJ_Remark' auto-complete='off'
                              type="textarea" :rows="3"></el-input>
                </el-form-item>


                <el-form-item label='状态' prop='SWJ_State'>
                    <!--<el-input v-model='addForm.SWJ_State' auto-complete='off'></el-input>-->
                    <template>
                        <el-radio-group v-model="addForm.SWJ_State">
                            <el-radio v-for="item in listState"
                                      :key="item.value"
                                      :label="item.value">{{item.text}}</el-radio>
                            <!--:value="item.value"-->
                        </el-radio-group>
                    </template>
                </el-form-item>



                <el-upload class="upload-demo"
                           hidden
                           drag
                           :on-success="handleUploadSuccess"
                           :action=actionUp
                           multiple>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>


            </el-form>
            <div slot='footer' class='dialog-footer'>
                <el-button @click.native='addFormVisible = false'>取消</el-button>
                <el-button type='primary' @click.native='addSubmit' :loading='addLoading'>提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>

    import util from '../../../util/date'
    import { testapi, getPage_DTSN_ShangWeiJi, getByID_DTSN_ShangWeiJi, edit_DTSN_ShangWeiJi, add_DTSN_ShangWeiJi, batchRemove_DTSN_ShangWeiJi, Remove_DTSN_ShangWeiJi } from '../../api/api';

    // action="https://jsonplaceholder.typicode.com/posts/"

    export default {
        data() {   // 全局变量
            return {

                actionUp: 'http://localhost:8081/api/FileUpload', // 'https://jsonplaceholder.typicode.com/posts/',//接口地址

                filters: { name: '' },
                users: [],
                total: 0,
                pageIndex: 1,
                pageSize: 20,

                sidx: null,   //  排序字段
                order: null,  //  排序方式， 正序\倒序

                listState: [{ text: '正常', value: 0 }, { text: '禁用', value: 1 }],
                listLoading: false,
                sels: [],//列表选中列
                addDialogFormVisible: false,
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {

                    SWJ_Name: [{ required: true, message: '请输入上位机名称', trigger: 'blur' }],
                    SWJ_Code: [{ required: true, message: '请输入上位机代码', trigger: 'blur' }],
                    SWJ_Address: [{ required: true, message: '请输入上位机地址 ', trigger: 'blur' }],
                    SWJ_IP: [{ required: true, message: '请输入上位机IP', trigger: 'blur' }],
                }
                ,
                //编辑界面数据
                editForm: {
                    ID: 0,
                    SWJ_Name: '',
                    SWJ_Code: '',
                    SWJ_Address: '',
                    SWJ_IP: '',
                    SWJ_Remark: '',
                    SWJ_State: 0,
                }
                ,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {

                    SWJ_Name: [{ required: true, message: '请输入上位机名称', trigger: 'blur' }],
                    SWJ_Code: [{ required: true, message: '请输入上位机代码', trigger: 'blur' }],
                    SWJ_Address: [{ required: true, message: '请输入上位机地址 ', trigger: 'blur' }],
                    SWJ_IP: [{ required: true, message: '请输入上位机IP', trigger: 'blur' }],
                }
                ,
                //新增界面数据
                addForm: {
                    SWJ_Name: '',
                    SWJ_Code: '',
                    SWJ_Address: '',
                    SWJ_IP: '',
                    SWJ_Remark: '',
                    SWJ_State: 0,
                }

            }
        },
        methods: {    // 全局方法
            //性别显示转换
            //formatSex: function (row, column) {
            //    return row.C_Value == 2 ? '男' : row.C_Value;
            //},
            //formatterCreateTime: function (row, column) {
            //    return (!row.C_CreateTime || row.C_CreateTime == '') ? '' : util.formatDate.format(new Date(row.C_CreateTime), 'yyyy-MM-dd');
            //},


            // handleUploadSuccess
            handleUploadSuccess(response, file, fileList) {
                console.log(response)
                console.log(file)
                console.log(fileList)
            },



            //  // 当前页
            handleCurrentChange(val) {
                this.pageIndex = val;
                this.getDtoPage();
            },
            // 每页数
            handleSizeChange(val) {
                this.pageSize = val;
                this.pageIndex = 1;
                this.getDtoPage();
            },
            // 测试
            sortKey(val) {
                console.log(val)
            },
            sortChange(column) {
                console.log(column.prop + "--" + column.order)
                this.sidx = column.prop
                this.order = column.order
                this.getDtoPage();
            },

            //获取用户列表
            getDtoPage() {
                let para = {
                    page: this.pageIndex,
                    pageSize: this.pageSize,
                    key: this.filters.name,
                    sidx: this.sidx,
                    order: this.order
                };
                this.listLoading = true;

                testapi();

                getPage_DTSN_ShangWeiJi(para).then((res) => {
                    console.log(res);
                    this.total = res.data.response.dataCount;
                    this.users = res.data.response.data;
                    this.listLoading = false;

                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { id: row.ID };
                    Remove_DTSN_ShangWeiJi(para).then((res) => {
                        if (util.isEmt.format(res)) {
                            this.listLoading = false;
                            return;
                        }
                        this.listLoading = false;
                        if (res.data.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
                        this.getDtoPage();
                    });
                }).catch((c) => {
                    console.log(c);
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;

                let para_getGetByID = { id: row.ID };

                getByID_DTSN_ShangWeiJi(para_getGetByID).then((getDto) => {

                    this.editForm = Object.assign({}, getDto.data.response);
                });
            },
            //显示新增界面
            handleAdd() {
                this.addFormVisible = true;
                this.addForm = {
                    SWJ_Name: '',
                    SWJ_Code: '',
                    SWJ_Address: '',
                    SWJ_IP: '',
                    SWJ_Remark: '',
                    SWJ_State: 0,
                }
                    ;
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                        }).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            console.log(para);
                            edit_DTSN_ShangWeiJi(para).then((res) => {

                                if (util.isEmt.format(res)) {
                                    this.editLoading = false;
                                    return;
                                }
                                if (res.data.success) {
                                    this.editLoading = false;

                                    this.$message({
                                        message: res.data.msg,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getDtoPage();
                                } else {
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                        }).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);

                            add_DTSN_ShangWeiJi(para).then((res) => {

                                if (util.isEmt.format(res)) {
                                    this.addLoading = false;
                                    return;
                                }

                                if (res.data.success) {
                                    this.addLoading = false;
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.getDtoPage();
                                }
                                else {
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {

                var ids = this.sels.map(item => item.ID).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning',
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    this.listLoading = true;
                    let para = { ids: ids };

                    batchRemove_DTSN_ShangWeiJi(para).then((res) => {
                        this.listLoading = false;

                        if (res.data.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
                        this.getDtoPage();
                        console.log(res)
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getDtoPage();
        },
        filters: {
            //tag类型
            getBindStatus(bindStatus) {
                const bindColor = {
                    1: 'danger',
                    0: 'success',

                };
                return bindColor[bindStatus]
            },
            //颜色名称
            getBindText(bindStatus) {
                const bindColor = {
                    1: '禁用',
                    0: '启用',

                };
                return bindColor[bindStatus]
            }

        }

    }

</script>

<style scoped>
</style>
